<?php

/**
 * 新闻管理
 */
namespace app\avikcm\controller;
use think\Db;
use think\facade\Request;

class News extends Common
{
    public function initialize()
    {
        parent::initialize();
    }


    // 新闻
    public function lists(){
        if(Request::isAjax()){
            $key = input('post.key');
            $type = input('post.type');
            $brand_id = input('post.brand_id');
            $this->assign('testkey', $key);
            $page = input('page', 1);
            $pageSize = input('limit', config('pageSize'));
            $where[] = ['del', '=', 0];
            if($type){
                $where[] = ['a.type','=',$type];
            }
            if($brand_id){
                $where[] = ['a.brand_id','=',$brand_id];
            }
            $paginate = ['list_rows'=>$pageSize,'page'=>$page];
            $list = db('news')->alias('a')->where($where) ->where('title', 'like', "%" . $key . "%")->order('update_time', 'desc')->paginate($paginate)->toArray();

            if($list['data']){
                $news_type = config('news_type');
                foreach ($list['data'] as $key=>$val){
                    $news_type_name = $news_type[$val['type']];
                    $category1_name = db('news_category')->where('id',$val['category1'])->value('title');
                    $brand_name = db('news_category')->where('id',$val['brand_id'])->value('title');
                    $list['data'][$key]['category_name'] = $category1_name;
                    if($val['category2']){
                        $category2_name = db('news_category')->where('id', $val['category2'])->value('title');
                        $list['data'][$key]['category_name'] .= ' > ' . $category2_name;
                    }
                    $list['data'][$key]['brand_name'] = $brand_name;
                    $list['data'][$key]['news_type_name'] = $news_type_name;
                }
            }
            return ['code'=>0,'msg'=>'成功','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
        }
        $brand = db('news_category')->field('id,title')->where(['pid'=>0,'del'=>0])->select();
        $this->assign('brand', $brand);
        $this->assign('news_type',config('news_type'));
        return $this->fetch();
    }

    // 添加新闻
    public function addLists(){
        if(Request::isAjax()){
            //构建数组
            $data = Request::except('file');
            $data['create_time'] = date("Y-m-d H:i:s",time());
            $data['update_time'] = date("Y-m-d H:i:s",time());
            db('news')->insert($data);
            $result['code'] = 1;
            $result['msg'] = '添加成功!';
            $result['url'] = url('lists');
            return $result;
        }else{
            $brand = db('news_category')->field('id,title')->where(['pid'=>0,'del'=>0])->select();
            $this->assign('brand', $brand);
            $this->assign('news_type',config('news_type'));
            $this->assign('title', '添加内容');
            $this->assign('info','null');
            return $this->fetch();
        }
    }

    // 修改新闻
    public function editLists(){
        if(Request::isAjax()){
            //构建数组
            $data = Request::except('file');
            $data['update_time'] = date("Y-m-d H:i:s");
            db('news')->update($data);
            $result['code'] = 1;
            $result['msg'] = '修改成功!';
            $result['url'] = url('lists');
            return $result;
        }else{
            $id = input('id');
            $info = db('news')->where('id',$id)->find();
            $category1 = db('news_category')->where(['del' => 0, 'pid' => $info['brand_id']])->select();
            $category2 = db('news_category')->where(['del' => 0, 'pid' => $info['category1']])->select();
            $brand = db('news_category')->field('id,title')->where(['pid' => 0, 'del' => 0])->select();
            $this->assign('category1', $category1);
            $this->assign('category2', $category2);
            $this->assign('brand', $brand);
            $this->assign('news_type', config('news_type'));
            $this->assign('info',json_encode($info,true));
            $this->assign('baseInfo',$info);
            $this->assign('title','修改内容');
            return $this->fetch('add_lists');
        }
    }
    // 置顶新闻
    public function topLists(){
        $id = input('id');
        db('news')->where('id',$id)->update(['top'=>1]);
        $result['code'] = 1;
        $result['msg'] = '置顶成功!';
        return $result;
    }
    // 删除新闻
    public function delLists(){
        $id = input('id');
        db('news')->where('id',$id)->update(['del'=>1]);
        return ['code'=>1,'msg'=>'删除成功！'];
    }
    // 批量删除新闻
    public function delAllLists(){
        $map[] =array('id','in',input('param.ids/a'));
        db('news')->where($map)->update(['del'=>1]);
        $result['msg'] = '删除成功！';
        $result['code'] = 1;
        return $result;
    }

    public function getCategory()
    {
        $id = input('id');
        $lists = db('news_category')->where(['del' => 0, 'pid' => $id])->select();
        return $lists;
    }

}